package com.flutter.jl_plugin;

import android.os.Handler;
import android.util.Log;

import com.jieli.jl_audio_decode.callback.OnDecodeStreamCallback;
import com.jieli.jl_audio_decode.exceptions.OpusException;
import com.jieli.jl_audio_decode.opus.OpusManager;
import com.jieli.jl_audio_decode.opus.model.OpusOption;
import com.jieli.logcat.JL_Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

public class OpusTools {
    static String TAG="OpusTools";
    public  static  OpusManager mOpusManager=null;
    public  static  Handler handler=new Handler();

    public static void initOpus(){
        try {
            mOpusManager=new OpusManager();
            Log.i(TAG, "initOpus: ");
        } catch (OpusException e) {
            JL_Log.w(TAG, "init", "Failed to init OpusManager. " + e);
            throw new RuntimeException(e);
        }
    }
    private static void decodeOpusStream(OpusManager manager, OpusOption option) {
        manager.startDecodeStream(option, new OnDecodeStreamCallback() {
            @Override
            public void onDecodeStream(byte[] data) {
                Log.d("zzc", "1写入PCM数据大小 >> " + data.length);
                //这里要结合讯飞去弄
                HashMap<String, Object> map = new HashMap<>();
                map.put("data",data);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        JlPlugin.Companion.getChannel().invokeMethod("onDecodeStream",map);
                    }
                });

            }

            @Override
            public void onStart() {
                Log.d("zzc", "解码开始 >> ");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        JlPlugin.Companion.getChannel().invokeMethod("onDecodeStart",null);
                    }
                });

            }

            @Override
            public void onComplete(String outFilePath) {
                Log.d("zzc", "解码结束 >> " + outFilePath);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        JlPlugin.Companion.getChannel().invokeMethod("onDecodeComplete",null);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {
                Log.e("zzc", "解码失败 >> " + code + ", " + message);
                HashMap<String, Object> map = new HashMap<>();
                map.put("code",code);
                map.put("message",message);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        JlPlugin.Companion.getChannel().invokeMethod("onDecodeError",map);
                    }
                });
            }
        });
    }


    static boolean isStartDecode=false;
    public static void startDecode(){
        if (mOpusManager != null) {
            if (!mOpusManager.isDecodeStream()) {
                OpusOption opusOption = new OpusOption();
                opusOption.setChannel(1);
                opusOption.setHasHead(false);
                opusOption.setPacketSize(40);
                opusOption.setSampleRate(16000);
                decodeOpusStream(mOpusManager,opusOption);
                Log.i(TAG, "startDecode1: ");
            }else{
                Log.i(TAG, "startDecode2: ");
            }
        }
        isStartDecode=true;
    }

    public static  void stopDecode(){
        if (null != mOpusManager) {
            mOpusManager.stopDecodeStream();
        }
        isStartDecode=false;
    }

    public static void decodeOpus(byte[] data){
        if(isStartDecode){
            if (mOpusManager.isDecodeStream()) {
                mOpusManager.writeAudioStream(data);
            }
        }


    }

    public static void release(){
        if (null != mOpusManager) {
            mOpusManager.stopDecodeStream();
            mOpusManager.stopEncodeStream();
            mOpusManager.release();
            mOpusManager = null;
        }
        isStartDecode=false;
    }
}
